package cn.jbolt.ec.order;

import cn.jbolt.base.JBoltBaseController;
import cn.jbolt.common.model.BecApp;
import cn.jbolt.common.model.BecRetailerOrder;
import cn.jbolt.ec.wx.GenService;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Inject;

public class EcPayController extends JBoltBaseController {

    @Inject
    OrderService orderService;
    BecApp becApp = GenService.getApp();
    @Inject
    PayService payService;

    public void init_param(int orderid,String openSession){
        JSONObject jo=new JSONObject();
        if (null!=getSessionAttr(openSession+"_openid")){
            String ips = this.getRequest().getRemoteAddr();
            BecRetailerOrder becRetailerOrder = orderService.findById(orderid);
            if(becRetailerOrder==null){
                renderJson("{ok:1}");
                return;
            }
            renderJson(PayService.gen_param(getSessionAttr(openSession+"_openid"),
                    becRetailerOrder.getCashAmount(),
                    becRetailerOrder.getOrderId(),
                    "jmd",
                    ips));
        }else {
            jo.put("ok",1001);
            jo.put("msg","无效Session");
            renderJson(jo);
        }

    }


    public void paid_handle(){

        System.out.println("res");
        String result =this.getRawData();
        System.out.println(result);


        renderText(payService.paid_handle(result));


    }

    public void check(int order_id,int coupon_id, boolean use_balance,String openSession){
        JSONObject jo=new JSONObject();



        if (null!=getSessionAttr(openSession+"_openid") ){
            renderJson(payService.check(order_id,coupon_id,use_balance,getSessionAttr(openSession+"_openid")));
        }else {
            jo.put("ok",1001);
            jo.put("msg","无效Session");
            renderJson(jo);
        }


    }
}
